﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBControl;

namespace ProjectX
{
    public partial class userCenter : Errorbase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["userNick"] == null)
                {
                    Response.Redirect("/Login.html");
                }
                else
                {
                    bindUserbase();
                    this.username.Text = Session["userNick"].ToString();
                    this.loginlink.Visible = false;
                    this.reglink.Visible = false;
                    this.logout.Visible = true;
                }
            }
        }

        private void bindUserbase()
        {
            string usermsg = "SELECT * FROM [userMSG] WHERE userName='" + Session["userNick"] + "'";
            SqlDbHelper sdh = new SqlDbHelper();
            this.userView.DataSource = sdh.ExecuteDataTable(usermsg);
            this.userView.DataBind();
        }
        protected void logout_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Write("<script language='javascript'>window.parent.location.href='/'</script>");
        }

        protected void userView_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            if (e.CommandName == "addpoint")
            {
                string updatepoint = "UPDATE UserPoint SET userpoint = userpoint + 10 WHERE userName = '" + Session["userNick"] + "'";
                SqlDbHelper sdh = new SqlDbHelper();
                sdh.ExecuteNonQuery(updatepoint);
                bindUserbase();
            }
        }

        protected void userView_DataBound(object sender, EventArgs e)
        {
            HiddenField hf = this.userView.FindControl("userpoint") as HiddenField;
            if (hf.Value == "0" )
            {
                this.userView.FindControl("getpoint").Visible = true;
            }
        }
    }
}